package com.ll.domain.classbase.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.app.api.req.ListSignupReqDTO;
import com.ll.base.util.ScalarUtil;
import com.ll.dal.po.SignupPO;
import com.ll.domain.classbase.enums.SingupStatusEnum;
import com.ll.domain.classbase.base.SignupBaseService;
import com.ll.dal.mapper.SignupMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SignupBaseServiceImpl extends ServiceImpl<SignupMapper, SignupPO> implements SignupBaseService {

    private final SignupMapper signupMapper;

    @Override
    public List<SignupPO> queryListByUserAndCourseIds(String dingTalkUserId, List<Long> courseIds) {
        return signupMapper.selectList(new LambdaQueryWrapper<SignupPO>()
                .eq(SignupPO::getDingTalkUserId, dingTalkUserId)
                .in(SignupPO::getCourseId, courseIds));
    }

    @Override
    public List<SignupPO> queryListByCourseId(Long courseId) {
        return signupMapper.selectList(new LambdaQueryWrapper<SignupPO>()
                .eq(SignupPO::getCourseId, courseId)
                .ne(SignupPO::getStatus, SingupStatusEnum.未报名));
    }

    @Override
    public List<SignupPO> queryList(ListSignupReqDTO reqDTO) {
        return signupMapper.selectList(new LambdaQueryWrapper<SignupPO>()
                .eq(ScalarUtil.isNotEmpty(reqDTO.getCourseId()) ,SignupPO::getCourseId, reqDTO.getCourseId())
                .in(reqDTO.getStatus() != null , SignupPO::getStatus, reqDTO.getStatus())
                .eq(ScalarUtil.isNotEmpty(reqDTO.getDingUid()), SignupPO::getDingTalkUserId, reqDTO.getDingUid())
                .in(ScalarUtil.isNotEmpty(reqDTO.getCourseIds()), SignupPO::getCourseId, reqDTO.getCourseIds())
        );
    }

    @Override
    public void updateStatus(Long signupId, SingupStatusEnum status) {


        SignupPO po = new SignupPO();
        po.setId(signupId);
        po.setStatus(status);
        signupMapper.updateById(po);
    }

    @Override
    public SignupPO getByUserIdAndCourse(String dingTalkUserId, Long courseId) {

        return signupMapper.selectOne(new LambdaQueryWrapper<SignupPO>()
                .eq(SignupPO::getDingTalkUserId, dingTalkUserId)
                .eq(SignupPO::getCourseId, courseId));
    }
}
